package com.liuguang.framework.utils;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public abstract class CacheDataBase {
    protected SharedPreferences sp;

    public enum ClearType {
        ClearTypeApp,
        ClearTypeUser
    }

    public abstract String getSpName();

    public CacheDataBase() {
        this.sp = null;
        this.sp = AppUtils.getApplication().getSharedPreferences(getSpName(), 0);
    }

    public boolean contains(String str) {
        try {
            return this.sp.contains(str);
        } catch (Exception e) {
            String obj = toString();
            StringBuilder sb = new StringBuilder();
            sb.append(str);
            sb.append("缓存读取失败");
            Log.e(obj, sb.toString(), e);
            return false;
        }
    }

    public void keep(String str, String str2) {
        try {
            Editor edit = this.sp.edit();
            edit.putString(str, str2);
            edit.commit();
        } catch (Exception e) {
            String obj = toString();
            StringBuilder sb = new StringBuilder();
            sb.append(str);
            sb.append("缓存存储失败");
            Log.e(obj, sb.toString(), e);
        }
    }

    public String read(String str) {
        try {
            return this.sp.getString(str, null);
        } catch (Exception e) {
            String obj = toString();
            StringBuilder sb = new StringBuilder();
            sb.append(str);
            sb.append("缓存读取失败");
            Log.e(obj, sb.toString(), e);
            return null;
        }
    }

    public String read(String str, String str2) {
        try {
            return this.sp.getString(str, str2);
        } catch (Exception e) {
            String obj = toString();
            StringBuilder sb = new StringBuilder();
            sb.append(str);
            sb.append("缓存读取失败");
            Log.e(obj, sb.toString(), e);
            return str2;
        }
    }

    public void keepInt(String str, int i) {
        try {
            Editor edit = this.sp.edit();
            edit.putInt(str, i);
            edit.commit();
        } catch (Exception e) {
            String obj = toString();
            StringBuilder sb = new StringBuilder();
            sb.append(str);
            sb.append("缓存存储失败");
            Log.e(obj, sb.toString(), e);
        }
    }

    public int readInt(String str, int i) {
        try {
            return this.sp.getInt(str, i);
        } catch (Exception e) {
            String obj = toString();
            StringBuilder sb = new StringBuilder();
            sb.append(str);
            sb.append("缓存读取失败");
            Log.e(obj, sb.toString(), e);
            return i;
        }
    }

    public void keepLong(String str, long j) {
        try {
            Editor edit = this.sp.edit();
            edit.putLong(str, j);
            edit.commit();
        } catch (Exception e) {
            String obj = toString();
            StringBuilder sb = new StringBuilder();
            sb.append(str);
            sb.append("缓存存储失败");
            Log.e(obj, sb.toString(), e);
        }
    }

    public long readLong(String str, long j) {
        try {
            return this.sp.getLong(str, j);
        } catch (Exception e) {
            String obj = toString();
            StringBuilder sb = new StringBuilder();
            sb.append(str);
            sb.append("缓存读取失败");
            Log.e(obj, sb.toString(), e);
            return j;
        }
    }

    public void keepBoolean(String str, boolean z) {
        try {
            Editor edit = this.sp.edit();
            edit.putBoolean(str, z);
            edit.commit();
        } catch (Exception e) {
            String obj = toString();
            StringBuilder sb = new StringBuilder();
            sb.append(str);
            sb.append("缓存存储失败");
            Log.e(obj, sb.toString(), e);
        }
    }

    public boolean readBoolean(String str, boolean z) {
        try {
            return this.sp.getBoolean(str, z);
        } catch (Exception e) {
            String obj = toString();
            StringBuilder sb = new StringBuilder();
            sb.append(str);
            sb.append("缓存读取失败");
            Log.e(obj, sb.toString(), e);
            return z;
        }
    }

    public boolean remove(String str) {
        return this.sp.edit().remove(str).commit();
    }

    public boolean clear() {
        return this.sp.edit().clear().commit();
    }

    public boolean clear(String str) {
        return AppUtils.getApplication().getSharedPreferences(str, 0).edit().clear().commit();
    }

    public static boolean clear(ClearType clearType) {
        switch (clearType) {
            case ClearTypeApp:
                CacheAppData.getInstance().clear();
                break;
            case ClearTypeUser:
                CacheUserData.getInstance().clear();
                break;
        }
        return true;
    }
}
